import { _decorator, Component, Node, tween, Vec3, UIOpacity } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MoveAndFadeOut')
export class MoveAndFadeOut extends Component {
    @property({ type: UIOpacity })
    private uiOpacity: UIOpacity | null = null;

    @property
    private moveDistance: number = 100; // 向上移动的距离
    @property
    private duration: number = 2; // 动画持续时间

    start() {
        if (this.uiOpacity) {
            this.fadeOutAndMove();
        }
    }

    private fadeOutAndMove() {
        // 设置初始透明度为255
        this.uiOpacity.opacity = 255;

        // 创建向上移动的动画
        const moveAction = tween(this.node)
            .to(this.duration, { position: new Vec3(this.node.position.x, this.node.position.y + this.moveDistance, this.node.position.z) })
            .start();

        // 创建渐隐的动画
        const fadeAction = tween(this.uiOpacity)
            .to(this.duration, { opacity: 0 })
            .start();

        // 等待两个动画完成后销毁节点
        Promise.all([moveAction, fadeAction]).then(() => {
            this.node.destroy(); // 动画完成后销毁节点
        });
    }
}